﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.Reflection;
using System.Xml.Serialization;

using Dynamic.Testing;
using Dynamic.Core.Utility;
using Dynamic.Core.Extentions;
using Dynamic.Testing.Learn;
using Dynamic.Testing.Model;
using System.Configuration;
using Dynamic.Data.Model;
using Dynamic.Data.Model.MetaData;
using Dynamic.Testing.T4;
using Dynamic.Testing.Config;
using Dynamic.Core.Office;
using Dynamic.Testing.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Diagnostics;

namespace Dynamic.Testing.App
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Items> li = new List<Items>();
            li.Add(new Items() { Name = "list1", num = 1 });
            li.Add(new Items() { Name = "list2", num = 2 });
            li.Add(new Items() { Name = "list3", num = 3 });
            li.Add(new Items() { Name = "list4", num = 4 });
            li.Add(new Items() { Name = "list5", num = 5 });
            li.Add(new Items() { Name = "list6", num = 6 });
            li.Add(new Items() { Name = "list7", num = 7 });
            li.Add(new Items() { Name = "list8", num = 8 });
            li.Where(x => x.num > 6).ForEach(i => { Console.WriteLine(string.Format("name={0},num={1}", i.Name, i.num)); });
            //try
            //{
            //    throw new Exception("test");
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

            //AD.ADTest.Go();

            //CodeDomTest.Go();
            //string str1 = null;
            //str1.IsInvalid();
            //string str2 = str1 ?? "2";
            //str1 = string.Empty;
            //str2 = str1 ?? "3";

            Console.WriteLine("Press any to continue....");
            Console.ReadKey();
        }

        enum TestEnum : long
        {
            aaa = 1,
            bb = 222,
            cc = 123456789000,
        }
    }

    public static class CodeDomTest
    {
        public static void Go()
        {

            /*MessageCodeGenerator mcg = new MessageCodeGenerator();
            XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load("Messages.xml");
            var codeObject = mcg.BuildCodeObject(doc);

            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            using (StreamWriter writer = new StreamWriter("messages.cs"))
            {
                provider.GenerateCodeFromCompileUnit(codeObject, writer, options);
            }

            provider = CodeDomProvider.CreateProvider("VisualBasic");
            using (StreamWriter writer = new StreamWriter("messages.vb"))
            {
                provider.GenerateCodeFromCompileUnit(codeObject, writer, options);
            }

            //Process.Start("messages.cs");
            //Process.Start("messages.vb");

            */
            Console.WriteLine(Messages.Validation.MandatoryField.Format("User Name"));
            Console.WriteLine(Messages.Validation.GreaterThan.Format("Age", 18));
            Console.WriteLine(Messages.Confirmation.ReallyDelete.Format("Order record"));
        }
    }

    public static class TestGo
    {
        public static void Go()
        {
            //ExcelHelper excle = new ExcelHelper();
            //excle.Open("D:\\Projects\\DynamicUnity\\Dynamic.Testing.App\\bin\\Debug\\结算分析-报表模板.xls");
            //excle.GetXFirstPosition("手续费占成交额比例", 5, 5, 20, "");
            //excle.GetYFirstPosition("手续费占成交额比例", 8, 5, 20, "sss");
            //excle.GetCellValue("手续费占成交额比例", 5, 6);
            //excle.GetCellValue("手续费占成交额比例", 5, 7);
            //excle.SetCellValue("Data1.1", 3, 3, 111);
            //excle.SetCellValue("Data1.1", 3, 4, 222);
            //excle.SetCellValue("Data1.1", 3, 5, 333);
            //excle.SetCellValue("手续费占成交额比例", 5, 9, 6666);
            //excle.SetCellValue("手续费占成交额比例", 5, 10, 777);
            //excle.SetCellValue("手续费占成交额比例", 5, 11, 888);
            //excle.SaveAs("D:\\Projects\\DynamicUnity\\Dynamic.Testing.App\\bin\\Debug\\结算分析-报表模板1.xls");
            //excle.Close();

            //MappingBaseTest.Go();
            //ConfigTest.Go();
            //AttributeMetadata att = new AttributeMetadata();
            //att.ColumnNumber = 1;
            //att.ColumnProperty = ColumnConstrainType.ForeignKey;

            //AttributeMetadata att2 = (AttributeMetadata)att.CreateInstance();

            Config.ConfigTest.Go();
            bool.Parse("true");
            bool.Parse("True");
            Console.Write(Convert.ToInt32(UriFormat.Unescaped));
            //BitArray 
            sS a = new sS();
            // a.GetType().GetCustomAttributes();
            Dictionary<string, object> ddd = a.GetPropertyValues();
            BitArray ba = new BitArray(1024);

            //Console.WriteLine(TestEnum.cc);
            //long l = (long)TestEnum.cc;
            //Console.WriteLine(l);
            //Assembly am = Assembly.Load("Dynamic.Testing");

            //MappingSample ms = (MappingSample)am.CreateInstance("Dynamic.Testing.Model.MappingSample");
            //Console.WriteLine(ms.Getss());
            //am = Assembly.Load("Dynamic.Testing");
            //ms = (MappingSample)am.CreateInstance("Dynamic.Testing.Model.MappingSample");
            //Console.WriteLine(ms.Getss());
            //Assembly am2 = Assembly.Load("Dynamic.Testing");

            //MappingSample ms2 = (MappingSample)am.CreateInstance("Dynamic.Testing.Model.MappingSample");
            //am2 = Assembly.Load("Dynamic.Testing");
            //Console.WriteLine(ms2.Getss());
            //ms2 = (MappingSample)am2.CreateInstance("Dynamic.Testing.Model.MappingSample");
            //Console.WriteLine(ms2.Getss());
            Dictionary<string, object> ttm = RefectionUtility.GetPropertyValues(new MappingSample());
            // Get the machine.config file.
            //Configuration machineConfig =
            //  ConfigurationManager.OpenMachineConfiguration();
            //// Get the machine.config file path.
            //ConfigurationFileMap configFile =
            //  new ConfigurationFileMap(machineConfig.FilePath);

            //// Map the application configuration file to the machine 
            //// configuration file.
            //Configuration config =
            //  ConfigurationManager.OpenMappedMachineConfiguration(
            //    configFile);

            //// Get the AppSettings section.
            //AppSettingsSection appSettingSection =
            //  (AppSettingsSection)config.GetSection("appSettings");
            //appSettingSection.SectionInformation.AllowExeDefinition =
            //    ConfigurationAllowExeDefinition.MachineToRoamingUser;

            //// Display the configuration file sections.
            //ConfigurationSectionCollection sections =
            //  config.Sections;

            //Console.WriteLine();
            //Console.WriteLine("Using OpenMappedMachineConfiguration.");
            //Console.WriteLine("Sections in machine.config:");

            //// Get the sections in the machine.config.
            //foreach (ConfigurationSection section in sections)
            //{
            //    string name = section.SectionInformation.Name;
            //    Console.WriteLine("Name: {0}", name);
            //}


            MappingBaseTest.Go();

            MyObject obj = new MyObject();
            XmlDocument xmldoc = XMLUtility<MyObject>.Serializer(obj);
            XmlNode xn = xmldoc.ParentNode;
            MyObject tmp = XMLUtility<MyObject>.Deserialize(xmldoc);

            int i = 0x2a;
            Console.Write(i);


            XmlDocument reval = new XmlDocument();

            XmlNode root = reval.CreateNode(XmlNodeType.Element, "root", "");
            XmlNode meta = reval.CreateNode(XmlNodeType.Element, "meta", "");
            XmlNode messages = reval.CreateNode(XmlNodeType.Element, "messages", "");

            reval.AppendChild(root);
            root.AppendChild(meta);
            root.AppendChild(messages);
            messages.AppendChild(meta);

            //StringBuilder sb = new StringBuilder();
            //XmlWriter w = XmlWriter.Create(sb);
            //XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
            //MyObject obj = new MyObject();

            //serializer.Serialize(w, obj);
            //XmlDocument xx = new XmlDocument();
            //xx.LoadXml(sb.ToString());
            //XmlNode xn = xx.SelectSingleNode("/MyObject/Birthday");
            //xn.OwnerDocument.LoadXml(reval.InnerXml);// = reval;
            reval.Save("c:\\dd.xml");

            SerializationTest.Go();

            //EnumUtilityTest.Go();
        }
    }

    public static class Helper
    {
        //List<Items> tt = new List<Items>();
        //   tt.Add(new Items() { num = 1, Name = "a1" });
        //   tt.Add(new Items() { num = 2, Name = "a2" });
        //   tt.Add(new Items() { num = 3, Name = "a3" });
        //   tt.Add(new Items() { num = 4, Name = "a4" });
        //   foreach (Items ii in tt.FindBy<Items>(item => item.num > 2))
        //   {
        //       Console.WriteLine(string.Format("num={0}; name={1}", ii.num, ii.Name));
        //   }

        public delegate bool Condtion<T>(T t);
        public static IEnumerable<T> FindBy<T>(this  IEnumerable<T> items, Condtion<T> condition)
        {
            foreach (T t in items)
            {
                if (condition(t))
                {
                    //C# 2.0里出现的一个关键字，返回一个迭代器
                    yield return t;
                }
            }
        }
    }

    public class Items
    {
        public int num;
        public string Name;
    }

    class sS : MappingBase
    {
    }
}
